package com.pets.web.service.impl;

import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.pets.web.dto.AllSumDto;
import com.pets.web.entity.PetsGoods;
import com.pets.web.entity.PetsOrders;
import com.pets.web.entity.PetsPlatform;
import com.pets.web.entity.PetsPlatformShare;
import com.pets.web.enums.OrderStatusEnums;
import com.pets.web.mapper.PetsPlatformShareMapper;
import com.pets.web.service.IPetOrdersService;
import com.pets.web.service.IPetsGoodsService;
import com.pets.web.service.IPetsPlatformService;
import com.pets.web.service.IPetsPlatformShareService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 平台分享 服务实现类
 * </p>
 *
 * @author zhurenwang
 * @since 2024-05-26
 */
@Service
public class PetsPlatformShareServiceImpl extends ServiceImpl<PetsPlatformShareMapper, PetsPlatformShare> implements IPetsPlatformShareService {

    @Autowired
    private IPetsGoodsService petsGoodsService;
    @Autowired
    private IPetOrdersService petOrdersService;
    @Autowired
    private IPetsPlatformService petsPlatformService;
    @Value("${share.url}")
    private String shareUrl;

    @Override
    public List<PetsPlatformShare> list(PetsPlatformShare petsPlatformShare) {
        MPJLambdaWrapper<PetsPlatformShare> wrapper = JoinWrappers.lambda(PetsPlatformShare.class)
                .selectAll(PetsPlatformShare.class)
                .selectSub(PetsOrders.class,
                        w -> w.selectSum(PetsOrders::getPayAmount)
                                .eq(PetsOrders::getPfCode, PetsPlatformShare::getPfCode)
                                .eq(PetsOrders::getGoodsId, PetsPlatformShare::getGoodsId)
                                .notIn(PetsOrders::getStatus, OrderStatusEnums.NO_PAY), PetsPlatformShare::getAmount)
                .selectSub(PetsOrders.class,
                        w -> w.selectSum(PetsOrders::getWeight)
                                .eq(PetsOrders::getPfCode, PetsPlatformShare::getPfCode)
                                .eq(PetsOrders::getGoodsId, PetsPlatformShare::getGoodsId)
                                .notIn(PetsOrders::getStatus, OrderStatusEnums.NO_PAY), PetsPlatformShare::getWeight)
                .selectSub(PetsOrders.class,
                        w -> w.selectCount(PetsOrders::getId)
                                .eq(PetsOrders::getPfCode, PetsPlatformShare::getPfCode)
                                .eq(PetsOrders::getGoodsId, PetsPlatformShare::getGoodsId)
                                .notIn(PetsOrders::getStatus, OrderStatusEnums.NO_PAY), PetsPlatformShare::getNum)
                .eq(Objects.nonNull(petsPlatformShare.getPlatformId()), PetsPlatformShare::getPlatformId, petsPlatformShare.getPlatformId())
                .eq(Objects.nonNull(petsPlatformShare.getGoodsId()), PetsPlatformShare::getGoodsId, petsPlatformShare.getGoodsId())
                .eq(Objects.nonNull(petsPlatformShare.getSysUserId()), PetsPlatformShare::getSysUserId, petsPlatformShare.getSysUserId());
        return baseMapper.selectJoinList(PetsPlatformShare.class, wrapper);
    }

    @Override
    public int add(PetsPlatformShare petsPlatformShare) {
        boolean exists = lambdaQuery()
                .eq(PetsPlatformShare::getGoodsId, petsPlatformShare.getGoodsId())
                .eq(PetsPlatformShare::getPlatformId, petsPlatformShare.getPlatformId())
                .exists();
        if (exists){
            throw new IllegalArgumentException("当前分享链接已存在");
        }
        PetsGoods petsGoods = petsGoodsService.getById(petsPlatformShare.getGoodsId());
        PetsPlatform petsPlatform = petsPlatformService.getById(petsPlatformShare.getPlatformId());
        petsPlatformShare.setGoodsName(petsGoods.getName());
        petsPlatformShare.setPlatformName(petsPlatform.getName());
        petsPlatformShare.setPfCode(petsPlatform.getPfCode());
        petsPlatformShare.setSysUserId(petsGoods.getSysUserId());
        String url = String.format(shareUrl, petsGoods.getId(), petsPlatform.getPfCode());
        petsPlatformShare.setUrl(url);
        return baseMapper.insert(petsPlatformShare);
    }

    @Override
    public int deleteById(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public AllSumDto getAll(Integer goodsId) {
        return petOrdersService.sumAll(goodsId);
    }
}
